package com.memo.fragment;

import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.fragment.app.Fragment;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.memo.Adpter.recy_adpter;
import com.memo.Bean.recybean;
import com.memo.Eventbean.homeone_evetbean;
import com.memo.Eventbean.hometwo_eventbean;
import com.memo.Interface.homeone_recy_adpter;
import com.memo.R;
import com.memo.SQL.memosql;
import com.memo.model.homeonemenu_model;
import com.memo.model.searchmodel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class fragment_homeone extends Fragment implements View.OnClickListener, homeone_recy_adpter {
    public RecyclerView homeone_recyclerview;
    public recy_adpter recy_adpter;
    public View viewsearch;
    public ImageView homeone_menu;
    public EditText homeone_search;
    public LinearLayout homeone_dellin;
    public ImageView homeone_delback;
    public TextView homeone_deltext;
    public ImageView homeone_del;
    public fragment_homeone(View viewsearch) {
        this.viewsearch = viewsearch;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_homeone, container, false);
        homeone_recyclerview = view.findViewById(R.id.homeone_recyclerview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        homeone_recyclerview.setLayoutManager(gridLayoutManager);
        recy_adpter = new recy_adpter(getActivity());
        homeone_recyclerview.setAdapter(recy_adpter);//设置适配器
        recy_adpter.setHomeone_recy_adpter(this);
        recy_adpter.Onstart();//启动适配器
        EventBus.getDefault().register(this);//启用EventBus
        homeone_menu=viewsearch.findViewById(R.id.homeone_menu);
        homeone_menu.setOnClickListener(this);
        homeone_search=viewsearch.findViewById(R.id.homeone_search);
        searchmodel searchmodel=new searchmodel(getActivity());
        searchmodel.editliner(homeone_search,recy_adpter);
        homeone_dellin=viewsearch.findViewById(R.id.homeone_dellin);
        homeone_delback=viewsearch.findViewById(R.id.homeone_delback);
        homeone_deltext=viewsearch.findViewById(R.id.homeone_deltext);
        homeone_del=viewsearch.findViewById(R.id.homeone_del);
        homeone_delback.setOnClickListener(this);
        homeone_del.setOnClickListener(this);
        return view;
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(homeone_evetbean event) {
        recy_adpter.refresh();
    }


    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.homeone_menu:
                homeonemenu_model homeonemenu_model=new homeonemenu_model(getActivity());
                homeonemenu_model.OnCreateMenu(homeone_menu);
                break;
            case R.id.homeone_delback:
                setdelin(false);
                recy_adpter.isdeletemodel=false;
                recy_adpter.refresh();
                recy_adpter.delall();
                EventBus.getDefault().post(new hometwo_eventbean(true));
                break;
            case R.id.homeone_del:
                recy_adpter.deleteall();
                setdelin(false);
                recy_adpter.isdeletemodel=false;
                recy_adpter.refresh();
                recy_adpter.delall();
                EventBus.getDefault().post(new hometwo_eventbean(true));
                Toast.makeText(getActivity(),"删除成功",Toast.LENGTH_SHORT).show();
                break;
        }
    }


    @Override
    public void setdelin(boolean is) {
        if (is){
            homeone_dellin.setVisibility(View.VISIBLE);
        }else {
            homeone_dellin.setVisibility(View.GONE);
        }
    }

    @Override
    public void setdelmatch(int match) {
        homeone_deltext.setText("删除项目至回收站|已选择"+match+"项");
    }

    @Override
    public void sethometwo(boolean hometwo) {
        EventBus.getDefault().post(new hometwo_eventbean(hometwo));
    }
}
